﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.WebServices;

namespace FRL.DebtChased.Repository
{

	/// <summary>
	/// Extensions to the AgentBankAccount model
	/// </summary>
	public partial class AgentBankAccount
	{

		/////////////////////////////////////////////////////////////
		// Operations

		/////////////////////////////////////////////////////////////
		// Static Operations

		public static List<BankAccountTransport> GetTransportList(Agent agent)
		{

			// Declare return variable
			List<BankAccountTransport> Ftmp = new List<BankAccountTransport>();

			// Loop through agent bank accounts
			foreach (AgentBankAccount aba in agent.AgentBankAccounts)
			{

				// Create transport object
				BankAccountTransport transport = BankAccountTransport.XmlFactory(aba.Xml);
				if (transport == null)
					throw new Exception("DebtChased internal error.  Cannot unwrap bank account.");
				transport.BankAccountId = aba.AgentBankAccountId;
				transport.InUse = true;

				// Add to list
				Ftmp.Add(transport);

			}

			// Return variable
			return Ftmp;

		}

		public static void InterCursorSetFromTransport(
			DebtChasedEntities ent,
			UserAction.Session session,
			Agent currentAgent,
			AgentTransport agentTransport,
			int iUserId
		)
		{

			// Loop through current list
			List<AgentBankAccount> currentList = currentAgent.AgentBankAccounts.ToList();
			foreach (AgentBankAccount currentBankAccount in currentList)
			{

				// Find the transport object
				BankAccountTransport transport = null;
				foreach (BankAccountTransport loop in agentTransport.BankAccounts)
				{
					if (loop.BankAccountId == currentBankAccount.AgentBankAccountId)
					{
						transport = loop;
						break;
					}
				}

				// If account found...
				if (transport != null)
				{

					// Wrap the bank account
					string strXml = transport.ToXml();

					// If record has changed...
					if (strXml.CompareWithoutUnicodePrefix(currentBankAccount.Xml, false) != 0)
					{

						// Update the account
						currentBankAccount.Xml = strXml;
						currentBankAccount.SearchFields = transport.GetSearchFields();

						// Update session
						session.AddChildDelta(currentBankAccount, transport.GetBestName(), null);

					}

				}
				else
				{

					// Delete the bank account
					ent.AgentBankAccounts.DeleteObject(currentBankAccount);

					// Get temp transport object
					BankAccountTransport Temp = BankAccountTransport.XmlFactory(currentBankAccount.Xml);

					// Update session
					session.AddChildDelta(currentBankAccount, Temp.GetBestName(), null);

				}

			}

			// Add new bank accounts
			foreach (BankAccountTransport transport in agentTransport.BankAccounts)
			{
#if DEBUG
				Debug.Assert(transport.Address != null);
#endif // #if DEBUG

				// Ignore all but new
				if (transport.BankAccountId != 0)
					continue;

				// Wrap the bank account
				string strXml = transport.ToXml();
				string strSearchFields = transport.GetSearchFields();

				// Create a new bank account
				AgentBankAccount newAccount = AgentBankAccount.CreateAgentBankAccount(0, 0, strSearchFields, strXml);
				newAccount.SearchFields = strSearchFields;
				newAccount.Agent = currentAgent;
				ent.AgentBankAccounts.AddObject(newAccount);

				// Update session
				session.AddChildDelta(newAccount, transport.GetBestName(), null);

			}

		}

	}

	/// <summary>
	/// Extensions to the ClientBankAccount model
	/// </summary>
	public partial class ClientBankAccount
	{

		/////////////////////////////////////////////////////////////
		// Operations

		/////////////////////////////////////////////////////////////
		// Static Operations

		public static List<BankAccountTransport> GetTransportList(Client client)
		{

			// Declare return variable
			List<BankAccountTransport> Ftmp = new List<BankAccountTransport>();

			// Loop through client bank accounts
			foreach (ClientBankAccount cba in client.ClientBankAccounts)
			{

				// Create transport object
				BankAccountTransport transport = BankAccountTransport.XmlFactory(cba.Xml);
				if (transport == null)
					throw new Exception("DebtChased internal error.  Cannot unwrap bank account.");
				transport.BankAccountId = cba.ClientBankAccountId;
				transport.InUse = true;

				// Add to list
				Ftmp.Add(transport);

			}

			// Return variable
			return Ftmp;

		}

		public static void InterCursorSetFromTransport(
			DebtChasedEntities ent,
			UserAction.Session session,
			Client currentClient,
			ClientTransport clientTransport,
			int iUserId
		)
		{

			// Loop through current list
			List<ClientBankAccount> currentList = currentClient.ClientBankAccounts.ToList();
			foreach (ClientBankAccount currentBankAccount in currentList)
			{

				// Find the transport object
				BankAccountTransport transport = null;
				foreach (BankAccountTransport loop in clientTransport.BankAccounts)
				{
					if (loop.BankAccountId == currentBankAccount.ClientBankAccountId)
					{
						transport = loop;
						break;
					}
				}

				// If account found...
				if (transport != null)
				{

					// Wrap the bank account
					string strXml = transport.ToXml();

					// If record has changed...
					if (strXml.CompareWithoutUnicodePrefix(currentBankAccount.Xml,false)!=0)
					{

						// Update the account
						currentBankAccount.Xml = strXml;
						currentBankAccount.SearchFields = transport.GetSearchFields();

						// Update session
						session.AddChildDelta(currentBankAccount, transport.GetBestName(), null);

					}

				}
				else
				{

					// Delete the bank account
					ent.ClientBankAccounts.DeleteObject(currentBankAccount);

					// Get temp transport object
					BankAccountTransport Temp = BankAccountTransport.XmlFactory(currentBankAccount.Xml);

					// Update session
					session.AddChildDelta(currentBankAccount, Temp.GetBestName(), null);

				}

			}

			// Add new bank accounts
			foreach (BankAccountTransport transport in clientTransport.BankAccounts)
			{
#if DEBUG
				Debug.Assert(transport.Address != null);
#endif // #if DEBUG

				// Ignore all but new
				if (transport.BankAccountId != 0)
					continue;

				// Wrap the bank account
				string strXml = transport.ToXml();
				string strSearchFields = transport.GetSearchFields();

				// Create a new bank account
				ClientBankAccount newAccount = ClientBankAccount.CreateClientBankAccount(0, 0, strSearchFields,strXml);
				newAccount.SearchFields = strSearchFields;
				newAccount.Client = currentClient;
				ent.ClientBankAccounts.AddObject(newAccount);

				// Update session
				session.AddChildDelta(newAccount, transport.GetBestName(), null);

			}

		}

	}

}
